from fastapi import FastAPI
from app.routers import TextRank_router
from starlette.middleware.base import BaseHTTPMiddleware
from fastapi import Request, HTTPException


# 这个类是一个中间件，用于验证请求的来源是否在允许的列表中
# 如果请求的来源不在列表中，将返回一个403错误
# http://localhost:5555 是允许的来源
class VerifyOriginMiddleware(BaseHTTPMiddleware):
    async def dispatch(self, request: Request, call_next):
        allowed_origins = ["http://127.0.0.1:5555"]
        origin = request.headers.get("origin")
        if origin not in allowed_origins:
            raise HTTPException(status_code=403, detail="Access denied")
        response = await call_next(request)
        return response


app = FastAPI()

# 将中间件添加到应用程序中
# 注释掉这一行，将禁用中间件及可以允许所有来源
# app.add_middleware(VerifyOriginMiddleware)

app.include_router(TextRank_router)
